package com.example.listview_2018;



        import android.content.Context;
 	import android.view.LayoutInflater;
 	import android.view.View;
 	import android.view.ViewGroup;
 	import android.widget.BaseAdapter;
    import android.widget.ImageView;
 	import android.widget.TextView;
 	import java.util.ArrayList;
 	import android.util.Log;

            /**
      * Created by yls on 2018/10/8.
      */

            public class StuAdapter extends BaseAdapter {
 	    private Context context;
 	    private ArrayList<Student> studentArrayList;

                public StuAdapter(Context context, ArrayList<Student> studentArrayList){
                this.context = context;
                this.studentArrayList = studentArrayList;
            }

                @Override
//                获取列表的数量
 	    public int getCount() {
                return studentArrayList.size();
            }

                @Override
//                获取某个位置的学生
 	    public Object getItem(int i) {
                return studentArrayList.get(i);
            }

                @Override
//                获取Id  用的是下标
                public long getItemId(int i) {
                return i;
            }
                int count = 0;
                @Override
 	    public View getView(int i, View view, ViewGroup viewGroup) {
//                LayoutInflater layoutInflater = LayoutInflater.from(context);
//                view = layoutInflater.inflate(R.layout.list_item,
//                                viewGroup, false) ;

                    Log.e("StuAdapter", "getview " + i + " count = " + count);

                                    // 先判断view是不是空 非空可以复用 不需要重新构建 节省内存
                                            if(view == null) {
                                                count++;
                                                LayoutInflater layoutInflater = LayoutInflater.from(context);
                                                view = layoutInflater.inflate(R.layout.list_item,
                                                        viewGroup, false);
//                                                使用ViewHolder减少findView的次数 提高效率
                                                StuViewHolder holder = new StuViewHolder();
                                                holder.headImg = view.findViewById(R.id.stu_img);
                                                holder.nameView = view.findViewById(R.id.stu_name);
                                                holder.ageView = view.findViewById(R.id.stu_age);
                                                view.setTag(holder);


                                            }

//                    ImageView headImg = view.findViewById(R.id.stu_img);
//                    TextView nameView = view.findViewById(R.id.stu_name);
//                    TextView ageView = view.findViewById(R.id.stu_age);

                    Student stu = studentArrayList.get(i);
//
//                    headImg.setImageResource(stu.getImgId());
//                    nameView.setText(stu.getName());
//                    ageView.setText(String.valueOf(stu.getAge()));

                    StuViewHolder holder = (StuViewHolder) view.getTag();
                 // 使用Holder保存子组件 避免每次使用时重复find
                    holder.headImg.setImageResource(stu.getImgId());
                    holder.ageView.setText(String.valueOf(stu.getAge()));
                    holder.nameView.setText(stu.getName());
                return view;
            }
 	}